package cn.net.cfss.fgbp.provider.manage.spi;

import cn.net.cfss.fgbp.base.constant.ErrCodeEnum;
import cn.net.cfss.fgbp.base.except.CodeException;
import cn.net.cfss.fgbp.base.service.FeignBaseService;
import cn.net.cfss.fgbp.provider.manage.biz.FscTenantUserBiz;
import cn.net.cfss.fgbp.spi.manage.constant.DeleteFlagEnum;
import cn.net.cfss.fgbp.spi.manage.dto.FscTenantDto;
import cn.net.cfss.fgbp.spi.manage.dto.FscTenantUserDto;
import cn.net.cfss.fgbp.spi.manage.inte.IFscTenantUserService;
import cn.net.cfss.fgbp.spi.manage.vo.FscTenantUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wutao
 * @version 1.0
 * @date 2020/9/11 14:57
 */
@RestController
@Slf4j
public class FscTenantUserSpi extends FeignBaseService implements IFscTenantUserService {

    @Autowired
    private FscTenantUserBiz fscTenantUserBiz;

    @Override
    public void addOrUpdate(FscTenantUserDto fscTenantUserDto) {
        if (fscTenantUserDto.getTenantId() == null || fscTenantUserDto.getUserId() == null) {
            log.error("新增或者修改用户绑定租户关系接口异常，传参为空");
            throw new CodeException(ErrCodeEnum.ERR_VAII);
        }
        FscTenantUserDto search = new FscTenantUserDto();
        search.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        search.setUserId(fscTenantUserDto.getUserId());
        FscTenantUserDto existDto = fscTenantUserBiz.selectOne(search);
        if (existDto == null) {
            //需要新增
            FscTenantUserDto add = new FscTenantUserDto();
            add.setId(fscTenantUserBiz.getUID());
            add.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
            add.setCreateBy(getUserId());
            add.setCreateTime(new Date());
            add.setUpdateBy(getUserId());
            add.setUpdateTime(new Date());
            add.setUserId(fscTenantUserDto.getUserId());
            add.setTenantId(fscTenantUserDto.getTenantId());
            fscTenantUserBiz.insert(add);
        } else {
            existDto.setTenantId(fscTenantUserDto.getTenantId());
            existDto.setUpdateBy(getUserId());
            existDto.setUpdateTime(new Date());
            fscTenantUserBiz.updateSelectiveById(existDto);
        }

    }

    @Override
    public Long queryTenantIdByUserid(Long userid) {
        FscTenantUserDto search = new FscTenantUserDto();
        search.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        search.setUserId(userid);
        FscTenantUserDto existDto = fscTenantUserBiz.selectOne(search);
        if (existDto == null) {
            return null;
        } else {
            return existDto.getTenantId();
        }
    }

    @Override
    public Map<Long, String> queryNameByUserIds(List<Long> userIds) {
        return fscTenantUserBiz.queryNameByUserIds(userIds);
    }

    @Override
    public Map<Long, FscTenantDto> queryTenantByUserIds(List<Long> userIds) {
        return fscTenantUserBiz.queryTenantByUserIds(userIds);
    }

    @Override
    public FscTenantUserVo queryByUserid(Long userId) {
        return fscTenantUserBiz.queryByUserid(userId);
    }

    @Override
    public void deleteByUserId(Long userId) {
        FscTenantUserDto fscTenantUserDto = new FscTenantUserDto();
        fscTenantUserDto.setUserId(userId);
        fscTenantUserDto.setUpdateTime(new Date());
        fscTenantUserDto.setUpdateBy(getUserId());
        fscTenantUserBiz.deleteByUserId(fscTenantUserDto);
    }
}
